package concurrencyPractice.chapter04;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序4-12 安全发布底层状态的车辆追踪器
 * @date 2023/12/28 17:49:02
 */
public class PublishingVehicleTracker {
    private final Map<String, SafePoint> locations;
    private final Map<String, SafePoint> unmodifiableMap;

    public PublishingVehicleTracker (Map<String, SafePoint> locations) {
        this.locations = new ConcurrentHashMap<String, SafePoint>(locations);
        this.unmodifiableMap = Collections.unmodifiableMap(this.locations);
    }

    public Map<String, SafePoint> getLocations() {
        return unmodifiableMap;
    }

    public SafePoint getLocation(String id) {
        return locations.get(id);
    }

    public void  setLocation(String id, int x, int y) {
        if(!locations.containsKey(id)) {
            throw new IllegalArgumentException("invalid vehicle name" + id);
        }
        locations.get(id).set(x, y);
    }
}
